use actix_web::middleware::Logger;
use actix_web::{error, get, App, HttpServer};
use derive_more::{Display, Error};
use log::info;
use std::io;

#[derive(Debug, Display, Error)]
#[display(fmt = "my error: {}", name)]
struct MyError {
    name: &'static str,
}

impl error::ResponseError for MyError {}

#[get("/")]
async fn index() -> Result<&'static str, MyError> {
    let err = MyError { name: "test error" };
    info!("{}", err);
    Err(err)
}

#[rustfmt::skip]
#[actix_web::main]
async fn main()->io::Result<()> {
    std::env::set_var("RUST_LOG","info");
    std::env::set_var("RUST_BACKTRACE","1");
    env_logger::init();

    HttpServer::new(||{
        let logger = Logger::default();
        App::new()
            .wrap(logger)
            .service(index)
    })
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}
